package cn.buk.tms.selfservice;

import cn.buk.tms.dao.CustomerDao;
import cn.buk.tms.dao.EnterpriseDao;
import cn.buk.tms.dao.FlightOrderDao;
import cn.buk.tms.dao.OrderNoGenerator;
import cn.buk.tms.entity.misc.AuthCodeInfo;
import cn.buk.tms.entity.misc.Customer;
import cn.buk.tms.entity.misc.Enterprise;
import cn.buk.tms.entity.order.flight.FlightOrder;
import cn.buk.tms.sc.SearchFlightOrderCriteria;
import cn.buk.tms.flight.util.FlightOrderUtil;
import cn.buk.tms.flight.dto.response.FlightOrderResponseDto;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 机票订单自助服务
 *
 * @author william
 * @date 2019-3-2
 */
@Component
public class FlightOrderSelfServiceImpl  implements FlightOrderSelfService {

    private static final Logger logger = LogManager.getLogger(FlightOrderSelfServiceImpl.class);


    @Autowired
    private FlightOrderDao flightOrderDao;

    @Autowired
    private EnterpriseDao enterpriseDao;

    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private OrderNoGenerator orderNoGenerator;

    /**
     * 根据客户的企业ID 获取在差旅公司系统中对应的客户ID
     * @param customerEnterpriseId
     * @return
     */
    private Customer getCustomerByCustomerEnterpriseId(final int customerEnterpriseId) {
        Enterprise enterprise = enterpriseDao.getEnterpriseById(customerEnterpriseId);
        if (enterprise == null || enterprise.getParentEnterprise() == 0) {
            logger.error("Enterprise data is error. -> " + enterprise);
            return null;
        }

        final int ownerId = enterprise.getParentEnterprise();

        return customerDao.getCustomerByCustomerEnterpriseId(ownerId, customerEnterpriseId);

    }


    /**
     * 查询机票订单
     * @param enterpriseId 客户在系统中的企业id
     * @param userId 客户在系统中的用户id
     * @param username
     * @param sc
     * @return
     */
    @Override
    public List<FlightOrderResponseDto> searchFlightOrders(final int enterpriseId, final int userId, final String username, SearchFlightOrderCriteria sc) {

        Customer customer = getCustomerByCustomerEnterpriseId(enterpriseId);
        if (customer == null) {
            logger.error("Customer data is error. -> " + enterpriseId);
            return new ArrayList<>();
        }

        final int ownerId = customer.getEnterpriseId();
        sc.setCustomerId(customer.getId());


        List<FlightOrder> flightOrders = flightOrderDao.searchOrder(ownerId, null, sc, false, null);

        if (flightOrders == null) {
          flightOrders = new ArrayList<>();
        }

        for (FlightOrder order : flightOrders) {
            FlightOrderUtil.clean(order);
        }

        List<FlightOrderResponseDto> dtos = new ArrayList<>();
        for(FlightOrder o: flightOrders) {
            dtos.add(FlightOrderResponseDto.createByFlightOrder(o));
        }

        return dtos;
    }

    /**
     * 通过授权码获取订单详情（适用于通过短信链接访问订单详情）
     * @param orderId
     * @param authCode 授权码
     * @param mobile
     * @return
     */
    @Override
    public FlightOrderResponseDto getFlightOrderDetailByAuthCode(final int orderId, final String authCode, final String mobile) {
        if (authCode == null) {
          return createFlightOrderDto(-1, "缺少授权码");
        }

        AuthCodeInfo authCodeInfo = enterpriseDao.getAuthCodeInfo(authCode);
        if (authCodeInfo == null) {
          return createFlightOrderDto(-2, "缺少有效授权码");
        }

        FlightOrder flightOrder = flightOrderDao.getOrderDetailById(orderId);
        if (flightOrder == null) {
          return createFlightOrderDto(-3, "未找到相关订单");
        }

        FlightOrderResponseDto dto = FlightOrderResponseDto.createByFlightOrder(flightOrder);

        dto.setPnrDetail(null);

        return dto;
    }

    private FlightOrderResponseDto createFlightOrderDto(int errCode, String errMsg) {
        FlightOrderResponseDto dto = new FlightOrderResponseDto();
        dto.setErrcode(errCode);
        dto.setErrmsg(errMsg);

        return dto;
    }

    /**
     *
     * @param enterpriseId
     * @param orderId
     * @return
     */
    @Override
    public FlightOrderResponseDto getFlightOrderDetail(final int enterpriseId, final int orderId) {
        FlightOrder flightOrder = flightOrderDao.getOrderDetailById(orderId);
        if (flightOrder == null || flightOrder.getCustomer() == null || flightOrder.getCustomer().getCustomerEnterpriseId() != enterpriseId) {
            logger.error(orderId);
            return createFlightOrderDto(-3, "未找到相关订单");
        }

        FlightOrderResponseDto dto = FlightOrderResponseDto.createByFlightOrder(flightOrder);

        dto.setPnrDetail(null);

        return dto;
    }

}
